﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class Main_AjaUtils_OnlineUsersGrid : System.Web.UI.Page
{
    /*page defaults*/
    OfficeOnlineUser currentUser;

    private void AuthenticateUser()
    {
        try
        {
            currentUser = (OfficeOnlineUser)Session["CurrentUser"];
        }
        catch (Exception ex)
        {
            //Response.Redirect("../login.aspx?urlReferrer=" + Request.Url);
        }

        if (currentUser == null)
        {
            //Response.Redirect("../login.aspx?urlReferrer=" + Request.Url);
        }
    }
    /*end page defaults*/


    protected void Page_Load(object sender, EventArgs e)
    {
        AuthenticateUser();        
        BindOnlineUsersTable();
    }

    private void BindOnlineUsersTable()
    {
        string command = "sp_tb_user_log_GetOnlineUsers @currentUser='" + currentUser.UserName + "'";
        SqlConnection sqlCon = new SqlConnection(DatabaseFunctions.GetConnectionString());
        sqlCon.Open();
        SqlDataAdapter sda = new SqlDataAdapter(command, sqlCon);

        DataSet dsReminders = new DataSet();
        sda.Fill(dsReminders);

        sqlCon.Close();

        hiddenOnlineUsersGrid.DataSource = dsReminders;
        hiddenOnlineUsersGrid.DataBind();
    }

    //private int StopReminder()
    //{
    //    Reminder rmdr = new Reminder();
    //    rmdr.ID = Request.QueryString["reminderID"];
    //    return rmdr.Stop();
    //}

    //private int PostponeReminder()
    //{
    //    Reminder rmdr = new Reminder();
    //    int min = Convert.ToInt32(Request.QueryString["min"]) + 5;
    //    rmdr.AlarmTime = Request.QueryString["hour"] + ":" + min.ToString() + ":00 " + Request.QueryString["AmPm"];
    //    rmdr.ID = Request.QueryString["reminderID"];
    //    return rmdr.Postpone();
    //}

    protected void onItemDataBound(object sender, DataGridItemEventArgs e)
    {
        switch (e.Item.ItemType)
        {
            case ListItemType.Item:
            case ListItemType.AlternatingItem:
                e.Item.Attributes.Add("style", "cursor:pointer");
                e.Item.Attributes.Add("ondblclick", "createChatRoom('" + DataBinder.Eval(e.Item.DataItem, "userName") + "','" + DataBinder.Eval(e.Item.DataItem, "userRealName") + "')");
                //e.Item.Attributes.Add("onclick", "createChatRoom('" + DataBinder.Eval(e.Item.DataItem, "userName") + "','" + DataBinder.Eval(e.Item.DataItem, "userRealName") + "')");
                e.Item.Attributes.Add("onclick", "selectOnlineUsersTableElement('" + e.Item.ClientID + "')");
                e.Item.Attributes.Add("userID", DataBinder.Eval(e.Item.DataItem, "userName").ToString());
                e.Item.Cells[1].Attributes.Add("title", DataBinder.Eval(e.Item.DataItem, "userRealName").ToString());
                break;
        }
    }
}
